<template>
  <div class="news-detail-page">
    <!-- 导航栏 -->
    <NavBar title="新闻详情" />
    
    <!-- 内容区域 -->
    <div class="page-content" v-if="news">
      <!-- 新闻详情 -->
      <div class="news-detail card">
        <!-- 新闻标题 -->
        <h1 class="news-title">{{ news.title }}</h1>
        
        <!-- 新闻信息 -->
        <div class="news-meta">
          <div class="meta-item">
            <van-icon name="clock-o" size="14" />
            <span>{{ formatTime(news.createTime) }}</span>
          </div>
          <div class="meta-item">
            <van-icon name="eye-o" size="14" />
            <span>{{ news.viewCount }} 阅读</span>
          </div>
          <div class="meta-item" v-if="news.author">
            <van-icon name="contact" size="14" />
            <span>{{ news.author }}</span>
          </div>
        </div>
        
        <!-- 新闻内容 -->
        <div class="news-content">
          <!-- 封面图片 -->
          <div class="cover-image" v-if="news.coverImage">
            <img :src="news.coverImage" alt="封面图片" @click="previewImage([news.coverImage], 0)" />
          </div>
          
          <!-- 正文内容 -->
          <div class="content-text" v-html="news.content"></div>
          
          <!-- 内容图片 -->
          <div class="content-images" v-if="news.images && news.images.length">
            <img 
              v-for="(img, index) in news.images" 
              :key="index"
              :src="img" 
              alt="内容图片"
              class="content-image"
              @click="previewImage(news.images, index)"
            />
          </div>
        </div>
        
        <!-- 标签 -->
        <div class="news-tags" v-if="news.tags && news.tags.length">
          <van-tag 
            v-for="tag in news.tags" 
            :key="tag"
            type="primary" 
            size="medium"
          >
            {{ tag }}
          </van-tag>
        </div>
        
        <!-- 底部操作 -->
        <div class="news-actions">
          <div class="action-stats">
            <span class="stat-item">
              <van-icon name="good-job-o" size="16" />
              {{ news.likeCount }}
            </span>
            <span class="stat-item">
              <van-icon name="chat-o" size="16" />
              {{ news.commentCount }}
            </span>
            <span class="stat-item">
              <van-icon name="share-o" size="16" />
              分享
            </span>
          </div>
          <div class="action-buttons">
            <van-button 
              size="small" 
              :type="isLiked ? 'primary' : 'default'"
              @click="toggleLike"
            >
              <van-icon :name="isLiked ? 'good-job' : 'good-job-o'" />
              {{ isLiked ? '已点赞' : '点赞' }}
            </van-button>
          </div>
        </div>
      </div>
      
      <!-- 评论区域 -->
      <div class="comments-section">
        <div class="comments-header">
          <h3>评论 ({{ comments.length }})</h3>
        </div>
        
        <!-- 评论列表 -->
        <div class="comments-list">
          <div 
            v-for="comment in comments" 
            :key="comment.id"
            class="comment-item card"
          >
            <div class="comment-header">
              <img :src="comment.userAvatar || '/images/default-avatar.png'" alt="头像" class="avatar" />
              <div class="comment-info">
                <div class="nickname">{{ comment.userNickname }}</div>
                <div class="time">{{ formatTime(comment.createTime) }}</div>
              </div>
            </div>
            <div class="comment-content">{{ comment.content }}</div>
          </div>
        </div>
        
        <!-- 评论输入 -->
        <div class="comment-input">
          <van-field
            v-model="commentText"
            placeholder="写下你的评论..."
            type="textarea"
            rows="2"
          >
            <template #button>
              <van-button 
                size="small" 
                type="primary" 
                @click="submitComment"
                :loading="commentLoading"
              >
                发送
              </van-button>
            </template>
          </van-field>
        </div>
      </div>
    </div>
    
    <!-- 加载状态 -->
    <van-loading v-else class="loading-center" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { showImagePreview, showToast } from 'vant'
import NavBar from '@/components/Common/NavBar.vue'
import { newsApi, commentApi } from '@/api'
import type { NewsItem, Comment } from '@/types'
import { formatTime } from '@/utils/time'

const route = useRoute()
const newsId = route.params.id as string

// 响应式数据
const news = ref<NewsItem | null>(null)
const comments = ref<Comment[]>([])
const commentText = ref('')
const commentLoading = ref(false)
const isLiked = ref(false)

// 加载新闻详情
const loadNews = async () => {
  try {
    const response = await newsApi.getDetail(newsId)
    news.value = response.data
  } catch (error) {
    showToast('加载失败')
    console.error('加载新闻详情失败:', error)
  }
}

// 加载评论
const loadComments = async () => {
  try {
    const response = await commentApi.getList(newsId)
    comments.value = response.data.list
  } catch (error) {
    console.error('加载评论失败:', error)
  }
}

// 提交评论
const submitComment = async () => {
  if (!commentText.value.trim()) {
    showToast('请输入评论内容')
    return
  }
  
  commentLoading.value = true
  
  try {
    await commentApi.create({
      postId: newsId,
      content: commentText.value
    })
    
    commentText.value = ''
    showToast('评论成功')
    loadComments()
  } catch (error) {
    showToast('评论失败')
    console.error('评论失败:', error)
  } finally {
    commentLoading.value = false
  }
}

// 切换点赞
const toggleLike = () => {
  isLiked.value = !isLiked.value
  showToast(isLiked.value ? '点赞成功' : '取消点赞')
}

// 预览图片
const previewImage = (images: string[], startPosition: number) => {
  showImagePreview({
    images,
    startPosition,
    closeable: true
  })
}

// 页面加载时获取数据
onMounted(() => {
  loadNews()
  loadComments()
})
</script>

<style scoped>
.news-detail-page {
  min-height: 100vh;
  background-color: #f1f1f1;
}

.page-content {
  padding: 16px;
}

.news-detail {
  margin-bottom: 16px;
}

.news-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
  margin-bottom: 16px;
}

.news-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #999;
}

.news-content {
  margin-bottom: 20px;
}

.cover-image {
  margin-bottom: 16px;
}

.cover-image img {
  width: 100%;
  border-radius: 8px;
  cursor: pointer;
}

.content-text {
  font-size: 16px;
  color: #333;
  line-height: 1.8;
  margin-bottom: 16px;
}

.content-text :deep(p) {
  margin-bottom: 12px;
}

.content-text :deep(img) {
  max-width: 100%;
  border-radius: 8px;
  margin: 12px 0;
}

.content-images {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.content-image {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
}

.news-tags {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}

.news-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.action-stats {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.comments-section {
  margin-bottom: 16px;
}

.comments-header {
  margin-bottom: 16px;
}

.comments-header h3 {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.comment-item {
  margin-bottom: 12px;
}

.comment-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.comment-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nickname {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.time {
  font-size: 12px;
  color: #999;
}

.comment-content {
  font-size: 14px;
  color: #333;
  line-height: 1.6;
}

.comment-input {
  background: white;
  border-radius: 8px;
  padding: 12px;
}

.loading-center {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>